<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="医生姓名" prop="doctorName">
        <el-input
          v-model="queryParams.doctorName"
          placeholder="请输入医生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="排班日期" prop="date">-->
<!--        <el-input-->
<!--          v-model="queryParams.date"-->
<!--          placeholder="请输入排班日期"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['schedules:schedules:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['schedules:schedules:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['schedules:schedules:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['schedules:schedules:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="schedulesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="排班编号" align="center" prop="scheduleId" />
      <el-table-column label="医生姓名" align="center" prop="doctorName" />
      <el-table-column label="科室名称" align="center" prop="deptName" />
      <el-table-column label="排班日期" align="center" prop="date" />


      <el-table-column label="上午时间" align="center" width="400">
        <template slot-scope="scope">
          <el-card style="background-color: #3a8ee6;" v-if="scope.row.morningStart==null">
            <p style="font-weight: bolder;color: #3A71A8;">无排班信息</p>
          </el-card>

          <el-card v-else style="background-color: #3a8ee6">
            <el-tag>
              <span style="font-weight: bolder;">开始时间：</span>
              <span>{{scope.row.morningStart.toString().slice(11,16) }}</span>
              <span>～</span>
              <span style="font-weight: bolder;">结束时间：</span>
              <span>{{scope.row.morningEnd.toString().slice(11,16) }}</span>
            </el-tag>
            <el-tag>
              <span style="font-weight: bolder;">上午号源数量：</span>
              <span>{{scope.row.morningQuota }}</span>
            </el-tag>
            <el-tag>
              <span style="font-weight: bolder;">剩余数量：</span>
              <span>{{scope.row.morningRemaining}}</span>
            </el-tag>
          </el-card>
        </template>
      </el-table-column>


      <el-table-column label="下午时间" align="center" width="400">
        <template slot-scope="scope">

          <el-card style="background-color: #3a8ee6;" v-if="scope.row.afternoonStart==null">
            <p style="font-weight: bolder;color: #3A71A8;">无排班信息</p>
          </el-card>

          <el-card v-else style="background-color: #3a8ee6">
            <el-tag>
              <span style="font-weight: bolder;">开始时间：</span>
              <span>{{scope.row.afternoonStart.toString().slice(11,16) }}</span>
              <span>～</span>
              <span style="font-weight: bolder;">结束时间：</span>
              <span>{{scope.row.afternoonEnd.toString().slice(11,16) }}</span>
            </el-tag>
            <el-tag>
              <span style="font-weight: bolder;">下午号源数量：</span>
              <span>{{scope.row.afternoonQuota }}</span>
            </el-tag>
            <el-tag>
              <span style="font-weight: bolder;">剩余数量：</span>
              <span>{{scope.row.afternoonRemaining}}</span>
            </el-tag>
          </el-card>
        </template>
      </el-table-column>


      <el-table-column label="夜间时间" align="center" width="400">
        <template slot-scope="scope">

          <el-card style="background-color: #3a8ee6;" v-if="scope.row.nightStart==null">
            <p style="font-weight: bolder;color: #3A71A8;">无排班信息</p>
          </el-card>

          <el-card v-else style="background-color: #3a8ee6">
            <el-tag>
              <span style="font-weight: bolder;">开始时间：</span>
              <span>{{scope.row.nightStart.toString().slice(11,16) }}</span>
              <span>～</span>
              <span style="font-weight: bolder;">结束时间：</span>
              <span>{{scope.row.nightEnd.toString().slice(11,16) }}</span>
            </el-tag>
            <el-tag>
              <span style="font-weight: bolder;">晚上号源数量：</span>
              <span>{{scope.row.nightQuota }}</span>
            </el-tag>
            <el-tag>
              <span style="font-weight: bolder;">剩余数量：</span>
              <span>{{scope.row.nightRemaining}}</span>
            </el-tag>
          </el-card>
        </template>
      </el-table-column>



      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['schedules:schedules:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['schedules:schedules:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改排班信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" style="margin: 0 auto;" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row gutter="20">
        <el-col :span="12">
          <el-form-item label="医生姓名" prop="doctorId">
           <el-select v-model="form.doctorId" placeholder="请选择医生" clearable>
             <el-option
               v-for="doctor in doctorList"
               :key="doctor.doctorId"
               :label="doctor.doctorName"
               :value="doctor.doctorId"
             />
           </el-select>
          </el-form-item>
        </el-col>
          <el-col :span="12">
            <el-form-item label="排班日期" prop="date">
              <el-input v-model="form.date" placeholder="请输入排班日期" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row gutter="20">
          <el-col :span="12">
            <el-form-item label="上午开始时间" prop="morningStart">
              <el-date-picker clearable
                              v-model="form.morningStart"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择上午开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上午结束时间" prop="morningEnd">
              <el-date-picker clearable
                              v-model="form.morningEnd"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择上午结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>





        <el-row gutter="20">
          <el-col :span="12">
            <el-form-item label="下午开始时间" prop="afternoonStart">
              <el-date-picker clearable
                              v-model="form.afternoonStart"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择下午开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="下午结束时间" prop="afternoonEnd">
              <el-date-picker clearable
                              v-model="form.afternoonEnd"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择下午结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>



        <el-row gutter="20">
          <el-col :span="12">
            <el-form-item label="夜间开始时间" prop="nightStart">
              <el-date-picker clearable
                              v-model="form.nightStart"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择夜间开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="夜间结束时间" prop="nightEnd">
              <el-date-picker clearable
                              v-model="form.nightEnd"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择夜间结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>



        <el-row gutter="20">
          <el-col span="8">
            <el-form-item label="上午号源数量" prop="morningQuota">
              <el-input v-model="form.morningQuota" placeholder="请输入上午号源数量" />
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item label="下午号源数量" prop="afternoonQuota">
              <el-input v-model="form.afternoonQuota" placeholder="请输入下午号源数量" />
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item label="夜间号源数量" prop="nightQuota">
              <el-input v-model="form.nightQuota" placeholder="请输入夜间号源数量" />
            </el-form-item>
          </el-col>
        </el-row>


        <el-row gutter="20">
          <el-col span="8">
            <el-form-item label="上午号源余量" prop="morningRemaining">
              <el-input v-model="form.morningRemaining" placeholder="请输入上午号源余量" />
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item label="下午号源余量" prop="afternoonRemaining">
              <el-input v-model="form.afternoonRemaining" placeholder="请输入下午号源余量" />
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item label="夜间号源余量" prop="nightRemaining">
              <el-input v-model="form.nightRemaining" placeholder="请输入夜间号源余量" />
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSchedules, getSchedules, delSchedules, addSchedules, updateSchedules } from "@/api/schedules/schedules"
import {getAllDoctors} from "@/api/doctors/doctors";

export default {
  name: "Schedules",
  data() {
    return {
      doctorList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排班信息表格数据
      schedulesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        doctorId: null,
        date: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        doctorId: [
          { required: true, message: "必须选择医生", trigger: "change" }
        ],
        date: [
          { required: true, message: "排班日期不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询排班信息列表 */
    getList() {
      this.loading = true
      listSchedules(this.queryParams).then(response => {
        this.schedulesList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        scheduleId: null,
        doctorId: null,
        date: null,
        morningStart: null,
        morningEnd: null,
        afternoonStart: null,
        afternoonEnd: null,
        nightStart: null,
        nightEnd: null,
        morningQuota: null,
        afternoonQuota: null,
        nightQuota: null,
        morningRemaining: null,
        afternoonRemaining: null,
        nightRemaining: null,
        createTime: null,
        updateTime: null,
        deleted: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scheduleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加排班信息"
      getAllDoctors().then(response => {
        console.log(response);
        this.doctorList = response.data;
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const scheduleId = row.scheduleId || this.ids
      this.open = true
      this.title = "修改排班信息"
      getSchedules(scheduleId).then(response => {
        this.form = response.data
      })
      getAllDoctors().then(response => {
        console.log(response);
        this.doctorList = response.data;
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.scheduleId != null) {
            updateSchedules(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addSchedules(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scheduleIds = row.scheduleId || this.ids
      this.$modal.confirm('是否确认删除排班信息编号为"' + scheduleIds + '"的数据项？').then(function() {
        return delSchedules(scheduleIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('schedules/schedules/export', {
        ...this.queryParams
      }, `schedules_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
